package LeetCode.month03.day68;

import java.util.*;

/**
 * @author 16778
 * @date 2025/10/7 9:44
 * @description
 */
public class No1488 {
    public int[] avoidFlood(int[] rains) {
        int n = rains.length;
        int[] res = new int[n];
        Arrays.fill(res,1);
        TreeSet<Integer> set = new TreeSet<>();
        Map<Integer,Integer> map = new HashMap<>();
        for (int i = 0; i < n; i++) {
            if(rains[i]==0){
                set.add(i);
            }else{
                res[i]=-1;
                if(map.containsKey(rains[i])){
                    Integer index = set.ceiling(map.get(rains[i]));
                    if(index==null){
                        return new int[0];
                    }
                    res[index] = rains[i];
                    set.remove(index);
                }
                map.put(rains[i],i);
            }
        }
        return res;
    }
}
